<?php


namespace Drupal\shusy_taxation\ViewsController\handler\filter;


use Drupal\shusy_taxation\Entity\ShusyTaxation;
use Exception;
use views_handler_filter_boolean_operator;

class ShusyTaxationInStoreViewsFilter extends views_handler_filter_boolean_operator {

  function query() {
    if (user_access('shusy_taxation access admin')) {
      return;
    }
    //设置不过滤
    if ($this->value == 0) {
      return;
    }
    if ($this->value == 1) {
      $this->ensure_my_table();
      $field = "$this->table_alias.$this->real_field";
      $or = db_or();
      $value = $this->getDbValues();
      if (empty($value)) {
        $or->condition($field, -1);
      }
      else {
        $or->condition($field, $value, 'IN');
      }
      $this->query->add_where($this->options['group'], $or);
    }
  }

  protected function getDbValues() {
    try {
      global $user;
      $list = [];
      $es = shusy_taxation_load_multiple(FALSE, ['uid' => $user->uid]);
      if (!empty($es)) {
        $e = reset($es);
        if ($e instanceof ShusyTaxation) {
          $list[] = $e->id;
        }
      }
      return $list;
    } catch (Exception $e) {
      return [];
    }
  }
}
